<?php

class HCommon {
	/**
	 * Rewrite URL, return the rewritten url based on RULES
	 * 
	 * RULES will be defined later
	 *
	 * @param string $page
	 * @param array() $params
	 */
	public static function navigateUrl($page, $params = null) {
		if ($params == null) {
			return HOST_NAME.'index.php?p='.$page;
		}
		else {
			$result = 'index.php?p='.$page;
			foreach ($params as $key=>$value) {
				$result .= '&'.$key.'='.$value;
			}			
			return HOST_NAME.$result;
		}
	}
	
	public static function resolveUrl($full_url) {
		return $full_url;
	}
	
	/**
	 * Avoid SQL Injection and Cross-site Attack
	 *
	 * @param string $string
	 * @return clean string
	 */
	public static function xss($string) {
		$string = stripslashes($string);
		$string = strip_tags($string);
		return $string;
	}
	
	public static function getUsername() {	
		return $_SESSION['HNS_SYSTEM']['username'];
	}
	
	public static function getUserId() {
		return $_SESSION['HNS_SYSTEM']['userid'];
	}
	
	public static function PHPSELF() {
		$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; 
		$protocol = self::strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;		$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
		
		return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; 
	}
	
	public static function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
	
	function deleteDir($dir) {
		$iterator = new RecursiveDirectoryIterator($dir);
		foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file)
		{
		  if ($file->isDir()) {
		     rmdir($file->getPathname());
		  } else {
		     unlink($file->getPathname());
		  }
		}
		rmdir($dir);
	}

}